		/*
		* Resources Icons
		*
		*/
		[Bindable]
        [Embed(source="username.gif")] 
        public var iconUser:Class;         
        [Bindable]
        [Embed(source="groups.gif")] 
        public var iconGroup:Class; 
        [Bindable]
        [Embed(source="icon_module.gif")] 
        public var iconApp:Class; 
        [Bindable]
        [Embed(source="blocks.gif")] 
        public var iconArea:Class; 
        [Bindable]
        [Embed(source="bullet_checked.gif")] 
        public var iconRight:Class; 


		/*
		* Tree functions
		*/
		[Bindable]
		public var refreshTree:Boolean = false;
				
        private function treeChanged(event:Event):void
        {
            var nodeApp:String = compLibTree.selectedItem.@app;
            var nodeL:String = compLibTree.selectedItem.@label;
            if (nodeApp != null && nodeApp != "")
            {
            	switch(nodeApp){
            		case "app":
            			tabNav.selectedIndex=0;
            			break;
            		case "area":
            			tabNav.selectedIndex=1;
            			break;
            		case "right":
            			tempRightTxt = compLibTree.selectedItem.@right_define_name;
            			tempRightId = compLibTree.selectedItem.@right_id;
            			if( conBttnUsers != null ) conBttnUsers.label = "Grant " + formTxt3.text + "  to " + tempRightTxt;
            			tabNav.selectedIndex=2;
            			break;
            		case "group":
            			tempRightTxt = compLibTree.selectedItem.@right_define_name;
            			tempRightId = compLibTree.selectedItem.@right_id;
            			tempGroupTxt = compLibTree.selectedItem.@label;
            			tempGroupId = compLibTree.selectedItem.@group_id;
            			disconBttn.toolTip = "Revoke group " + nodeL + "\n from " + tempRightTxt;
            			if( joinBttn != null ) joinBttn.label = "Join " + formTxt3.text + "  to " + tempGroupTxt;  
	    	            if( detachBttn != null ) detachBttn.label = "Remove " + formTxt3.text + "  from " + tempGroupTxt;  
            			tabNav.selectedIndex=3;
            			break;
            		case "user":
            			tempRightTxt = compLibTree.selectedItem.@right_define_name;
            			tempRightId = compLibTree.selectedItem.@right_id;
            			disconBttn.toolTip = "Revoke user " + nodeL + "\n from " + tempRightTxt;
            			tabNav.selectedIndex=4;
            			break;
            		case "groupuser":
            			tempGroupTxt = compLibTree.selectedItem.@group_define_name;
            			tempGroupId = compLibTree.selectedItem.@group_id;
            			disconBttn.toolTip = "Remove user " + nodeL + "\n from " + tempGroupTxt;
            			tabNav.selectedIndex=4;
            			break;
            	}
            }
            else
            {
                compLibTree.expandItem(compLibTree.selectedItem, true, true);
            }
        }

        private function explorerLoaded():void
        {
            explorerTree = XML(explorer.lastResult.node);
            populateTree();
        }


        //we do not use data binding because the timing 
        private function populateTree():void
        {
            compLibTree.dataProvider = explorerTree;
            refreshTree = true;
            //callLater( renderTree );
        }
        private function renderTree():void
        {
        	// Open all children
        	if ( refreshTree  ){        		
            	refreshTree = false;
            	expandTree();
         }
        }
		private function expandTree():void
		{
        	compLibTree.invalidateList();
            compLibTree.validateNow();
                for (var i:int = 0; i < compLibTree.dataProvider.length ; i ++){
					if ( expandCB.selected  ){
                           compLibTree.expandChildrenOf(compLibTree.dataProvider[i], true);
				     } else {
                           compLibTree.expandChildrenOf(compLibTree.dataProvider[i], false);
     				 }
                }
		}
	
		/*
		* Data grid handling
		*
		*/
           import mx.collections.ArrayCollection;
           import mx.controls.Alert;
           import mx.managers.CursorManager;
           import mx.rpc.events.FaultEvent;
           import mx.rpc.events.InvokeEvent;
           import mx.rpc.events.ResultEvent;
           import mx.utils.ArrayUtil;

            
            [Bindable]
            public var appsCol:ArrayCollection;
            [Bindable]
            public var areasCol:ArrayCollection;
            [Bindable]
            private var rightsCol:ArrayCollection;
            [Bindable]
            private var groupsCol:ArrayCollection;
            [Bindable]
            private var usersCol:ArrayCollection;
            [Bindable]
            private var logCol:ArrayCollection;


            
            // Gets called when HTTPService is invoked to
            // request the XML.
            private function appsInvokeHandler(event:InvokeEvent):void
            {
                CursorManager.setBusyCursor();
            }
            private function appsFaultHandler(event:FaultEvent):void
            {
                Alert.show (event.fault.message);    
                CursorManager.removeBusyCursor();
            }
            
            // Gets called when the XML is successfully loaded.
            private function grantResultHandler(event:ResultEvent):void
            {
                statusText.text = event.result.lua.node.error ;
                CursorManager.removeBusyCursor();
            }

            // If result is more than 1 node, we have ArrayCollection
            // Otherwise we should convert:
            private function getResultArrayCollection(event:ResultEvent):ArrayCollection
            {
				if (event.result.lua.node is ArrayCollection) {
  					return event.result.lua.node;
				} else if (event.result.lua.node is Object) {
   					return new ArrayCollection(ArrayUtil.toArray(event.result.lua.node)); 
				} else {
					return null
				}
            }
            
            private function appsResultHandler(event:ResultEvent):void
            {
                appsCol = getResultArrayCollection(event);
                CursorManager.removeBusyCursor();
            }
            private function areasResultHandler(event:ResultEvent):void
            {
                areasCol = getResultArrayCollection(event);
                CursorManager.removeBusyCursor();
            }
            private function rightsResultHandler(event:ResultEvent):void
            {
                rightsCol = getResultArrayCollection(event);
                CursorManager.removeBusyCursor();
            }
            private function groupsResultHandler(event:ResultEvent):void
            {
                groupsCol = getResultArrayCollection(event);
                CursorManager.removeBusyCursor();
            }
            private function usersResultHandler(event:ResultEvent):void
            {
                usersCol = getResultArrayCollection(event);
                CursorManager.removeBusyCursor();
            }
            private function logResultHandler(event:ResultEvent):void
            {
                logCol = getResultArrayCollection(event);
                //CursorManager.removeBusyCursor();
	        	logTA.text = logCol.getItemAt(0).line.toString();
            }

            private function dgChangeHandler():void 
            {
                formId.label = dg.selectedItem.application_id;
                formTxt.text = dg.selectedItem.application_define_name;
            }
            private function dg0ChangeHandler():void 
            {
                formId0.label = dg0.selectedItem.area_id;
                formTxt0.text = dg0.selectedItem.area_define_name;
                formTxtApp0.text = dg0.selectedItem.application_id;
            }
            private function dg1ChangeHandler():void 
            {
                tempRightId = formId1.label = dg1.selectedItem.right_id;
                tempRightTxt = formTxt1.text = dg1.selectedItem.right_define_name;
                formTxtArea1.text = dg1.selectedItem.area_id;
            }
            private function dg2ChangeHandler():void 
            {
                tempGroupId = formId2.label = dg2.selectedItem.group_id;
                tempGroupTxt = formTxt2.text = dg2.selectedItem.group_define_name;
                conBttnGroups.label = "Grant group_id=" + formId2.label + " to " + tempRightTxt;  
            }
            private function dg3ChangeHandler():void 
            {
            	if( dg3.selectedItem.isactive == '1' ){
                	formTxtActive3.selected = true;
                } else {
                	formTxtActive3.selected = false;
                }            		
                formId3.label = dg3.selectedItem.auth_user_id;
                formTxt3.text = dg3.selectedItem.handle;
                formTxtName3.text = dg3.selectedItem.name;
                formTxtEmail3.text = dg3.selectedItem.email;
                formTxtAdd3.text = dg3.selectedItem.address;
                formTxtTelx3.text = dg3.selectedItem.tel1;
                formTxtTely3.text = dg3.selectedItem.tel2;
                conBttnUsers.label = "Grant " + formTxt3.text + "  to " + tempRightTxt;  
                joinBttn.label = "Join " + formTxt3.text + "  to " + tempGroupTxt;  
                detachBttn.label = "Remove " + formTxt3.text + "  from " + tempGroupTxt;  
            }


            private function joinGroupCommit():void 
            {
            	if ( tempGroupId == null  ){
		            alert = Alert.show("Select appropiate group for Join action ", "Don't know what to do...", Alert.OK);
					return;
            	}
       		    httpAction = "addU";
            	httpData1 = dg3.selectedItem.handle;
            	httpData2 = tempGroupId;
           		grant.send();
           		explorer.send();
            }
            private function detachGroupCommit():void 
            {
            	if ( tempGroupId == null  ){
		            alert = Alert.show("Select appropiate group for Remove User from Group action ", "Don't know what to do...", Alert.OK);
					return;
            	}
       		    httpAction = "removeU";
            	httpData1 = dg3.selectedItem.handle;
            	httpData2 = tempGroupId;
           		grant.send();
           		explorer.send();
            }
            private function treeConnectGCommit():void 
            {
            	if ( tempRightId == null  ){
		            alert = Alert.show("Select appropiate right and group for Grant action ", "Don't know what to do...", Alert.OK);
					return;
            	}
       		    httpAction = "grantG";
            	httpData1 = formId2.label;
            	httpData2 = tempRightId;
           		grant.send();
           		explorer.send();
            }
            private function treeConnectUCommit():void 
            {
            	if ( tempRightId == null  ){
		            alert = Alert.show("Select appropiate right and user for Grant action ", "Don't know what to do...", Alert.OK);
					return;
            	}
       		    httpAction = "grantU";
            	httpData1 = dg3.selectedItem.handle;
            	httpData2 = tempRightId;
           		grant.send();
           		explorer.send();
            }
            private function treeDisconnectCommit():void 
            {
            	var nodeG:String = compLibTree.selectedItem.@group_id;
            	var nodeU:String = compLibTree.selectedItem.@auth_user_id;
            	var nodeR:String = compLibTree.selectedItem.@right_id;
            	if (nodeG != null && nodeG != "" && nodeU != null && nodeU != "" ){
       		    	httpAction = "removeU";
            		httpData1 = nodeU;
            		httpData2 = nodeG;
            	} else if (nodeG != null && nodeG != ""){
       		    	httpAction = "revokeG";
            		httpData1 = nodeG;
	           		httpData2 = nodeR;
            	} else if (nodeU != null && nodeU != ""){
       		    	httpAction = "revokeU";
            		httpData1 = nodeU;
	           		httpData2 = nodeR;
            	} else { //Either user from group, or user, or group. Nothing else
                	alert = Alert.show("Select appropiate group or user for Revoke action ", "Don't know what to do...", Alert.OK);
            		return;
            	}
           		grant.send();
           		explorer.send();
            }

            
            
            private function dgCommit():void 
            {
            	if (rbInsert.selected){
            		httpAction = "add";
            		httpData1 = formTxt.text;
             	} else if(rbDelete.selected && dg.selectedIndex != -1 ){
            		httpAction = "del";
            		httpData1 = formId.label;
             	} else if(rbEdit.selected == true && dg.selectedIndex != -1 ){
       		        httpAction = "edit";
            		httpData1 = formId.label;
            		httpData2 = formTxt.text;
             	}
           		apps.send();
           		explorer.send();
            }
            private function dg0Commit():void 
            {
            	if (rbInsert0.selected){
            		httpAction = "add";
            		httpData1 = formTxt0.text;
            		httpData2 = formTxtApp0.text;
             	} else if(rbDelete0.selected && dg0.selectedIndex != -1 ){
            		httpAction = "del";
            		httpData1 = formId0.label;
             	} else if(rbEdit0.selected == true && dg0.selectedIndex != -1 ){
       		        httpAction = "edit";
            		httpData1 = formId0.label;
            		httpData2 = formTxt0.text;
             	}
           		areas.send();
           		explorer.send();

            }
            private function dg1Commit():void 
            {
            	if (rbInsert1.selected){
            		httpAction = "add";
            		httpData1 = formTxt1.text;
            		httpData2 = formTxtArea1.text;
             	} else if(rbDelete1.selected ){ //&& formId1.label != "" ){
            		httpAction = "del";
            		httpData1 = formId1.label;
             	} else if(rbEdit1.selected == true && formId1.label != "" ){
       		        httpAction = "edit";
            		httpData1 = formId1.label;
            		httpData2 = formTxt1.text;
             	}
           		rights.send();
           		explorer.send();
            }
            private function dg2Commit():void 
            {
            	if (rbInsert2.selected){
            		httpAction = "add";
            		httpData1 = formTxt2.text;
             	} else if(rbDelete2.selected && dg2.selectedIndex != -1 ){
            		httpAction = "del";
            		httpData1 = formId2.label;
             	} else if(rbEdit2.selected == true && dg2.selectedIndex != -1 ){
       		        httpAction = "edit";
            		httpData1 = formId2.label;
            		httpData2 = formTxt2.text;
             	}
           		groups.send();
           		explorer.send();
            }
            private function dg3Commit():void 
            {
            	if (rbInsert3.selected){
            		httpAction = "add";            		
            		httpData2 = formTxt3.text;
            		httpData3 = formTxtName3.text;
            		httpData4 = formTxtEmail3.text;
            		httpData5 = formTxtAdd3.text;
            		httpData6 = formTxtTelx3.text;
            		httpData7 = formTxtTely3.text;
            		if( formTxtActive3.selected == true ){
   		            	httpData8 = '1';
                	} else {
   		            	httpData8 = '0';
                	}
            		httpData9 = formTxtPwd3.text;
             	} else if(rbDelete3.selected && dg3.selectedIndex != -1 ){
            		httpAction = "del";
            		httpData1 = dg3.selectedItem.handle;
             	} else if(rbEdit3.selected == true && dg3.selectedIndex != -1 ){
       		        httpAction = "edit";
            		httpData1 = dg3.selectedItem.handle; // You can not edit handle... It's a primary key
            		httpData2 = formTxt3.text;
            		httpData3 = formTxtName3.text;
            		httpData4 = formTxtEmail3.text;
            		httpData5 = formTxtAdd3.text;
            		httpData6 = formTxtTelx3.text;
            		httpData7 = formTxtTely3.text;
            		if( formTxtActive3.selected == true ){
   		            	httpData8 = '1';
                	} else {
   		            	httpData8 = '0';
                	}
            		httpData9 = formTxtPwd3.text;
             	}
           		users.send();
           		
           		explorer.send();
            	
            }

            private function logCommit():void 
            {
           		httpData1 = linesSlider.value.toString();
	        	applog.send();

            }



			/* 
			* Context Menu
			*
			*/
            [Bindable]
            private var cm:ContextMenu;

            private var alert:Alert;

            private function contextMenuInit():void {
                var cmi:ContextMenuItem = new ContextMenuItem("Connect tree with list item", true);
                cmi.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, contextMenuItem_menuItemSelect);
                var cmi2:ContextMenuItem = new ContextMenuItem("Disconnect tree with list item", true);
                cmi2.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, contextMenuItem_menuItemSelect);

                cm = new ContextMenu();
                cm.hideBuiltInItems();
                cm.customItems = [cmi,cmi2];
                cm.addEventListener(ContextMenuEvent.MENU_SELECT, contextMenu_menuSelect);
            }

            private function contextMenu_menuSelect(evt:ContextMenuEvent):void {
                dg.selectedIndex = 2;
            }

            private function contextMenuItem_menuItemSelect(evt:ContextMenuEvent):void {
                var obj:Object = dg.selectedItem;
                var obj2:Object = compLibTree.selectedIndex;
                alert = Alert.show("Property A: " + obj2 + "\n" + "Property B: " + compLibTree.selectedItem.@app, "Hm...", Alert.OK);
            }

			/*
			*
			* Timer
			*
			*/
			private function timerStatus():void {
            var myTimer:Timer = new Timer(20000, 0);
            myTimer.addEventListener("timer", timerHandler);
            myTimer.start();
        	}

	        private function timerHandler(event:TimerEvent):void {
	        	statusText.text = luaVersion;
	        	//applog.send();
	        	//logTA.text = logTA.text. + "\n" + 'ff' ;
	        	//logTA.text = logTA.text + "\n" + logCol.getItemAt(0).line.toString();
	         }
			
